package com.eims.sls_app.ui;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.toolbox.RequestParams;
import com.contans.view.MainHOngbaoListVo;
import com.eims.hongbao.fragment.HongbaoListAdapter;
import com.eims.hongbao.fragment.LuckeyDialog;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.NetWork;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;

public class MainHongbaoListActivity extends MyBaseActivity implements OnClickListener, OnItemClickListener {
	public static final String TAG = "MainHongbaoListActivity";
	private ImageButton ibBack;
	private MyHandler myHandler;

	private ListView MainHongbaoList;

	private HongbaoListAdapter adapter;

	private List<MainHOngbaoListVo> lists;

	private Dialog dialog;
	LuckeyDialog.Builder builder;

	private String getHongbaoId, isother;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数

	private String resultDes;
	private int getposition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_hongbao_list);
		myHandler = new MyHandler();
		init();

	}

	private void init() {

		Bundle bundle = getIntent().getExtras();
		lists = (List<MainHOngbaoListVo>) bundle.getSerializable("hb");
		isother = bundle.getString("isother");
		// Log.e("ss", lists.toString());
		ibBack = (ImageButton) findViewById(R.id.btn_back_con);
		ibBack.setOnClickListener(this);
		MainHongbaoList = (ListView) findViewById(R.id.listview_hongbao);
		adapter = new HongbaoListAdapter(this, lists);
		MainHongbaoList.setAdapter(adapter);
		MainHongbaoList.setOnItemClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_back_con:

			ProgressDialogBar.startProgress(MainHongbaoListActivity.this, "sff", false);
			myHandler.sendEmptyMessageDelayed(4, 100);

			break;

		default:
			break;
		}
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 5:

				if (NetWork.isConnect(MainHongbaoListActivity.this)) {
					MyAsynTask asynTask = new MyAsynTask();
					asynTask.execute();
				} else {
					Toast.makeText(MainHongbaoListActivity.this, "网络连接不可用！", Toast.LENGTH_SHORT).show();
				}

				break;
			case -1:

				if (resultDes.contains("error")) {
					Toast.makeText(MainHongbaoListActivity.this, "获取信息失败！", Toast.LENGTH_SHORT).show();

				} else {
					myHandler.sendEmptyMessage(1);

					if (resultDes.contains("恭喜")) {
						lists.get(getposition).setYl("1");
						if (isother.equals("1")) {
							MyCommonLotteryInfo_jc_other.getMyInfoVo.getHb().get(getposition).setYl("1");
						} else {
							MyCommonLotteryInfo_jc.getMyInfoVo.getHb().get(getposition).setYl("1");
						}

					}

					adapter.notifyDataSetChanged();
					builder.SetDes(resultDes, lists.get(getposition).getLimit());
				}

				break;
			case 1:
				// Toast.makeText(MainHongbaoListActivity.this, "1", Toast.LENGTH_SHORT).show();
				// dialog.dismiss();

				break;
			case 4:
				ProgressDialogBar.stopProgress();
				MainHongbaoListActivity.this.finish();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		final String name = lists.get(position).getUname();
		// String name = topicBeans.get(position).name;
		final String isname;
		if (isMobileNO(name)) {
			isname = name.substring(0, 3) + "****" + name.substring(7, name.length());

		} else {
			isname = name;

		}

		builder = new LuckeyDialog.Builder(this, R.style.Dialog);// ����style�е�Diaog
		builder.setName(isname);
		builder.setURl(lists.get(position).getHeadUrl());
		builder.setDEs(lists.get(position).getTilte());
		getHongbaoId = lists.get(position).getId();
		getposition = position;

		builder.SetSy(lists.get(position).getSy(), lists.get(position).getYl());

		builder.setOpenButton("", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// Intent intent = new Intent(this,Open.class);
				// startActivity(intent);
				switch (which) {

				case 1:
					myHandler.sendEmptyMessageDelayed(5, 600);
					break;
				case 2:

					Bundle bundle = new Bundle();
					bundle.putString("id", getHongbaoId);

					bundle.putString("name", lists.get(getposition).getUname());
					bundle.putString("url", lists.get(getposition).getHeadUrl());
					bundle.putString("des", lists.get(getposition).getTilte());
					bundle.putString("count", lists.get(getposition).getTotal());

					Intent intent = new Intent(MainHongbaoListActivity.this, HongbaoListInfoActivity.class);
					intent.putExtras(bundle);
					startActivity(intent);
					// Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
					break;
				default:
					break;
				}

				// dialog.dismiss();
			}
		});

		builder.setCloseButton("", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int i) {
				dialog.dismiss();
			}
		});

		dialog = builder.create();
		Window dialogWindow = dialog.getWindow();
		dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
		WindowManager m = getWindowManager();
		Display d = m.getDefaultDisplay();
		WindowManager.LayoutParams p = dialogWindow.getAttributes();
		p.height = (int) (d.getHeight() * 0.75);
		p.width = (int) (d.getWidth() * 0.85);
		dialogWindow.setAttributes(p);

		if (lists.get(position).getYl().equals("0")) {
			dialog.show();

		} else {

			Bundle bundle = new Bundle();
			bundle.putString("id", getHongbaoId);

			bundle.putString("name", lists.get(getposition).getUname());
			bundle.putString("url", lists.get(getposition).getHeadUrl());
			bundle.putString("des", lists.get(getposition).getTilte());
			bundle.putString("count", lists.get(getposition).getTotal());

			Intent intent = new Intent(MainHongbaoListActivity.this, HongbaoListInfoActivity.class);
			intent.putExtras(bundle);
			startActivity(intent);
		}
	}

	class MyAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-1";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "113";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(MainHongbaoListActivity.this);
			info = getHongbaoId;// 16
			// info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);

			// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
			String key = App.key;
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			String[] values = { opt, auth, info };
			Log.e(TAG, "晒单" + RequestParams.convertParams(MainHongbaoListActivity.this, opt, info));
			String result = HttpUtils.doPost(App.names, values, App.path);

			Log.e(TAG, "晒单：" + result);

			resultDes = result;

			return error;
		}

		@Override
		protected void onPostExecute(String result) {

			myHandler.sendEmptyMessage(Integer.parseInt(result));

			super.onPostExecute(result);
		}
	}
}
